package com.ysc.plugin.utility.photo

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Bitmap
import android.graphics.Matrix
import android.media.ExifInterface
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.ysc.plugin.common.LocalBroadcastAction
import com.ysc.plugin.common.abstract.Manager
import com.ysc.plugin.common.abstract.ManagerModule
import com.ysc.plugin.common.abstract.Plugin
import com.ysc.plugin.utility.photo.clip.ClipPhoto
import com.ysc.plugin.utility.photo.open.OpenPhoto
import com.ysc.plugin.utility.receiver.UtilityReceiver
import java.io.IOException

class PhotoManger(): Manager() {
    companion object {
        @SuppressLint("StaticFieldLeak")
        private var instance: PhotoManger?= null
        fun getInstance(): PhotoManger? {
            instance ?: synchronized(this) {
                instance ?: PhotoManger().apply { instance = this }
            }
            return instance
        }
    }

    init {
        instance = this
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        for (pluginModule in getManagerModule()){
            if(pluginModule.value.getActivityResult())
            {
                pluginModule.value.onActivityResult(requestCode, resultCode, data)
            }
        }
    }

    override fun onDestroy() {

    }

    override fun getHelp(): String {
        return ""
    }

    override fun getName(): String {
        return "PhotoManger"
    }

    override fun getVersion(): String {
        return "1.0.0"
    }

    override fun initManagerModule() {
        addManagerModule(OpenPhoto(super.getActivity()!!))
        addManagerModule(ClipPhoto(super.getActivity()!!))
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        for (pluginModule in getManagerModule()){
            if(pluginModule.value.getPermissionsResult())
                pluginModule.value.onRequestPermissionsResult(requestCode, permissions, grantResults)
        }
    }

    fun readPictureDegree(path: String):Int {
        var degree = 0
        try {
            var exifInterface = ExifInterface(path);
            var orientation = exifInterface.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL
            );
            when (orientation) {
                ExifInterface.ORIENTATION_ROTATE_90 -> degree = 90
                ExifInterface.ORIENTATION_ROTATE_180 -> degree = 180
                ExifInterface.ORIENTATION_ROTATE_270 -> degree = 270
            }
        } catch (e: IOException) {
            e.printStackTrace();
        }
        return degree;
    }

    fun rotaingImageView(angle: Float, bitmap: Bitmap) : Bitmap {
        var returnBm: Bitmap? = null
        // 根据旋转角度，生成旋转矩阵
        var matrix =  Matrix()
        matrix.postRotate(angle)
        try {
            // 将原始图片按照旋转矩阵进行旋转，并得到新的图片
            returnBm = Bitmap.createBitmap(
                bitmap,
                0,
                0,
                bitmap.getWidth(),
                bitmap.getHeight(),
                matrix,
                true
            );
        } catch (e: OutOfMemoryError) {
        }
        if (returnBm == null) {
            returnBm = bitmap
        }
        if (bitmap != returnBm) {
            bitmap.recycle()
        }
        return returnBm
    }
}